import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
import { createHtmlPlugin } from 'vite-plugin-html';
import setupExtend from 'vite-plugin-vue-setup-extend';
import * as path from 'path';
import { viteMockServe } from 'vite-plugin-mock';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: env['VITE_BASE_DIR'],
    resolve: {
      //设置别名
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    plugins: [
      dynamicImportVars({
        include: 'src/pages/'
      }),
      viteMockServe({
        mockPath: 'mock',
        enable: true
      }),
      vue(),
      createHtmlPlugin({
        minify: true,
        inject: {
          data: {
            title: loadEnv(mode, process.cwd())['VITE_APP_TITLE']
          }
        }
      }),
      Components({
        dirs: ['src/components', 'src/pages'],
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
        resolvers: [
          ElementPlusResolver(),
          IconsResolver({
            enabledCollections: ['mdi'],
            prefix: 'icon'
          })
        ]
      }),
      Icons({
        compiler: 'vue3',
        autoInstall: true,
        iconCustomizer(_collection, _icon, props){
          props.class = 'svg-icon'
          props.width = '1em'
          props.height = '1em'
        }
      }),
      setupExtend()
    ],
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true
        }
      }
    },
    server: {
      open: true
      // host: 'localhost',
      // port: 8111, //启动端口
      // hmr: {
      //     port: 8111
      // }
      // 设置代理
      // proxy: {
      //     '/api': {
      //         target: 'http://10.10.202.93:8185/', // 测试环境
      //         changeOrigin: true,
      //         rewrite: (path: string) => path.replace(/^\/api/, '')
      //     }
      // }
    }
  };
});
